package com.burke.gwt.client;

import java.util.ArrayList;

import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;

public class FloaterGroup {
	  final double width;
	  final double height;
	  final int numFloaters;
	  Image personImg;

	  ArrayList<Floater> floaters;

	  boolean imageLoaded = false;;
	  
	  
	  public FloaterGroup(double width, double height) {
	    this.width = width;
	    this.height = height;
	    numFloaters = 0;

	    floaters = new ArrayList<Floater>();
	    
	    // init image
	    personImg = new Image("../ANDROID-ICON.png");
	    ImageElement imageElement = (ImageElement) personImg.getElement().cast();
	  
	          
	    personImg.setVisible(false);
	    imageLoaded= true;
//	    RootPanel.get().add(personImg); // image must be on page to fire load
	  }
	  
	  public void update(Person target) { //Vector t, Rect r) {		  
		  ArrayList<Floater> temp = new ArrayList<Floater>();
		  
		  for (Floater f: floaters) {
			  Vector t = new Vector();
			  t.x = target.targetRect.left + 10;
			  t.y = target.targetRect.top + 10;
			  f.update(t);
			  
			// collision detection
			  if (target.targetRect.isInside(f.pos.x, f.pos.y)) {
				  f = null;
				  target.excite();
			  }
			  else {
				  temp.add(f);
			  }
			  
			  floaters = temp;
			  
		  }
		  
		  
		  
	  }
	  
	  public void draw(Context2d context) {
	    if (!imageLoaded) {
	      return;
	    }
	    
	    for (Floater f: floaters) {
			  f.draw(context);
		  }
	  }
	  
	  public void addFloater(int x, int y, int type) {
		 
		  
		  ImageElement imageElement = (ImageElement) personImg.getElement().cast();
		  
		  Floater f = new Floater(imageElement);
		  f.pos.x = x;
		  f.pos.y = y;
		  floaters.add(f);
		  
	  }
	  
}
